﻿using System.ComponentModel;
using Airborne.ScrumPoker.Model.TeamFoundation;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace Airborne.ScrumPoker.Module.TeamFoundation.Model
{
    public class ProductBacklogItem : ITeamWorkItem, INotifyPropertyChanged
    {
        #region Private Members
        private const string BusinessPriorityField = "Conchango.TeamSystem.Scrum.BusinessPriority";
        private const string DeliveryOrderField = "Conchango.TeamSystem.Scrum.DeliveryOrder";
        private const string DescriptionField = "System.Description";
        private const string EstimatedEffortField = "Conchango.TeamSystem.Scrum.EstimatedEffort";


        #endregion

        #region Constructor
        public ProductBacklogItem(WorkItem workItem)
        {
            WorkItem = workItem;
        }
        #endregion

        #region Properties
        private WorkItem WorkItem { get; set; }


        public string Title
        {
            get
            {
                return WorkItem.Title;
            }
        }

        public int BusinessPriority
        {
            get
            {
                return (int)WorkItem.Fields[BusinessPriorityField].Value;
            }
        }

        public int DeliveryOrder
        {
            get
            {
                return (int)WorkItem.Fields[DeliveryOrderField].Value;
            }
        }

        public double? EstimatedEffort
        {
            get
            {
                return (double?)WorkItem.Fields[EstimatedEffortField].Value;
            }
            set
            {
                VerifyWorkItemOpen(WorkItem);
                WorkItem.Fields[EstimatedEffortField].Value = value;
                RaisePropertyChanged("EstimatedEffort");
            }
        }

        public string Description
        {
            get
            {
                return (string)WorkItem.Fields[DescriptionField].Value;
            }
            set
            {
                VerifyWorkItemOpen(WorkItem);
                WorkItem.Fields[DescriptionField].Value = value;
                RaisePropertyChanged("Description");
            }
        }

        public string Comment
        {
            get
            {
                return WorkItem.History;
            }
            set
            {
                VerifyWorkItemOpen(WorkItem);
                WorkItem.History =  value;
                RaisePropertyChanged("Comment");
            }
        }
        #endregion

        #region PropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventArgs args = new PropertyChangedEventArgs(propertyName);
            OnPropertyChanged(args);
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion

        #region Methods
        public void Save()
        {
            if (ShouldSaveWorkItem(WorkItem))
            {
                WorkItem.Save();
            }
        }

        private static void VerifyWorkItemOpen(WorkItem workItem)
        {
            Guard.IsNotNull(workItem, "workItem");
            if (!workItem.IsOpen)
            {
                workItem.Open();
            }
        }

        private static bool ShouldSaveWorkItem(WorkItem workItem)
        {
            Guard.IsNotNull(workItem, "workItem");
            return (workItem.IsOpen && workItem.IsDirty);
        }
        #endregion

    }
}
